package com.service.approval.interceptor;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.service.approval.exception.ApprovalParamCheckException;
import com.service.approval.model.ApprovalInfo;
import com.service.approval.model.InsuredInfo;
import com.service.approval.model.PolicyHolderInfo;
import com.service.approval.model.ProductInfo;
import com.service.approval.util.DataModelBuildUtil;

@Service("b")
public class BParamAnalysisIntercepter implements ApprovalIntercepter{

	@Override
	public ApprovalContext execute(ApprovalContext approvalContext) throws ApprovalParamCheckException {
		String approvalInfoSource = approvalContext.getApprovalInfoSource();
		JSONObject parseObject = JSONObject.parseObject(approvalInfoSource);
		JSONObject productInfoJson = parseObject.getJSONObject("productInfo");
		JSONObject insuredInfoJson = parseObject.getJSONObject("insuredInfo");
		JSONObject policyHolderJson = parseObject.getJSONObject("policyHolder");
		String merchantNo = parseObject.getString("merchantNo");
		String merchantOrderNo = parseObject.getString("merchantOrderNo");
		try {
			ProductInfo productInfo = DataModelBuildUtil.buildModel(productInfoJson, ProductInfo.class);
			InsuredInfo insuredInfo = DataModelBuildUtil.buildModel(insuredInfoJson, InsuredInfo.class);
			PolicyHolderInfo policyHolderInfo = DataModelBuildUtil.buildModel(policyHolderJson, PolicyHolderInfo.class);
			ApprovalInfo approvalInfo = new ApprovalInfo(merchantNo, merchantOrderNo, productInfo, policyHolderInfo, insuredInfo);
			approvalContext.setApprovalInfo(approvalInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return approvalContext;
	}

}
